<?php
function article_head($db, &$headers, $translate) {
	if (!isset($_GET['id']))
		throw new Exception("Missing article ID.");
	// Get page tags and title for meta tag
	$data = $db->fetchRow("SELECT id,title,tags FROM articles WHERE id = " . $db->quote($_GET['id']) . " AND published = 1");
	if ($data == null)
		throw new Exception("Content is not available.");
	// Increase hit count but only once per session
	$visitorNamespace = new Zend_Session_Namespace('gcmsvisitor');
	if (!isset($visitorNamespace->visited['article'][(int)$_GET['id']])) {
		$db->update('articles', array('hits' => new Zend_Db_Expr('(hits+1)')), 'id = '.$db->quote($_GET['id']));
		$visitorNamespace->visited['article'][(int)$_GET['id']] = true;
	}
	$headers->headTitle($data['title']);
	if (!empty($data['tags']))
		$headers->headMeta()->setName('keywords', $data['tags']); // Override default keywords
}

function article_main($db, &$view, $translate) {
	require_once './libs/phpbb.php';
	global $config;
	if (!empty($config['forumdir']))
		$phpbb = new Zend_PHPBB3($config['forumdir']);
	$view->getEngine()->load_filter('output', 'geshi');
	$data = $db->fetchRow("SELECT * FROM articles WHERE id = " . $db->quote($_GET['id']) . " AND published = 1");
	$data['tags'] = tag_builder($data['tags']);
	$page = preg_split('/\{gcmsPageBreak(.*)\}/i', $data['content'], -1, PREG_SPLIT_NO_EMPTY);
	if (preg_match_all('/ title="(.*)"\}/i', $data['content'], $matches) > 0)
		array_unshift($matches[1], $data['title']);
	else
		$matches[1][0] = $data['title'];
	if (isset($phpbb) && $phpbb->get_approved($data['comments']))
		$data['comments'] = array('fid' => $config['forumid'], 'tid' => $data['comments'], 'count' => $phpbb->get_count($data['comments']));
	$view->assign('ptitle', $matches[1]);
	$view->assign('page', $page);
	$view->assign('pagecount', count($page));
	$view->assign('data', $data);
	if (!isset($_GET['page']))
		$_GET['page'] = 1;
	$view->display('article.tpl');
}

function article_print($db, &$view, $translate) {
	$view->getEngine()->load_filter('output', 'geshi');
	$data = $db->fetchRow("SELECT * FROM articles WHERE id = " . $db->quote($_GET['id']) . " AND published = 1");
	// clean page tags
	$data['content'] = preg_replace('/\{gcmsPageBreak(.*)\}/i', '', $data['content']);
	$view->assign('title', $data['title']);
	$view->assign('content', $data['content']);
	$view->assign('published', $data['pubdate']);
	$view->assign('modified', $data['moddate']);
	$view->display('file:../../../modules/article/print.tpl');
}
?>
